////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	Crytek Character Animation source code
//	
//	
//  Contains:
//	This file contains just prototype-code for testing new features
//	As soon as this stuff is mature enough we will move it into production-tools (in this case the resource-compiler)
//	
//	
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "CharacterManager.h"


void CAnimationSet::CreateSkeletonArray( std::vector< std::vector<DebugJoint> >& arrSkeletons, uint32 nAnimID )
{
	Diag33 temp;
	const ModelAnimationHeader* pAnim = GetModelAnimationHeader(nAnimID);
	GlobalAnimationHeaderCAF& rGAH = g_AnimationManager.m_arrGlobalCAF[pAnim->m_nGlobalAnimId];

	f32 duration = rGAH.m_fEndSec - rGAH.m_fStartSec;

	f64 PosesPerKey = (1.0/60.0);
	uint32 HowManyPoses = uint32(duration/PosesPerKey);//= HowManyPoses; 
	if (HowManyPoses==0)
		HowManyPoses=1;

	arrSkeletons.resize(HowManyPoses);
	uint32 numJoints = m_pModel->m_ModelSkeleton.m_arrModelJoints.size();
	for(uint32 i=0; i<HowManyPoses; i++)
		arrSkeletons[i].resize(numJoints);

	const CModelJoint* pModelJoint = &m_pModel->m_ModelSkeleton.m_arrModelJoints[0];
	for (uint32 s=0; s<HowManyPoses; s++) 
	{
		//evaluate all controllers for this animation
		f32 newtime = (1.0f/HowManyPoses)*s; 
		for (uint32 j=0; j<numJoints; j++)
		{
			arrSkeletons[s][j].m_RelativeQuat=m_pModel->m_ModelSkeleton.m_poseData.m_pJointsRelative[j];
			IController* pController = g_AnimationManager.m_arrGlobalCAF[pAnim->m_nGlobalAnimId].GetControllerByJointCRC32(pModelJoint[j].m_nJointCRC32); 
			if (pController)
				pController->GetOP(	rGAH.NTime2KTime(newtime), arrSkeletons[s][j].m_RelativeQuat.q, arrSkeletons[s][j].m_RelativeQuat.t);
		}

		//calculate absolute joints
		arrSkeletons[s][0].m_AbsoluteQuat = arrSkeletons[s][0].m_RelativeQuat;
		for (uint32 i=1; i<numJoints; i++)
		{
			int16 idx = arrSkeletons[s][i].m_idxParent =	m_pModel->m_ModelSkeleton.m_arrModelJoints[i].m_idxParent;
			arrSkeletons[s][i].m_AbsoluteQuat	= arrSkeletons[s][idx].m_AbsoluteQuat * arrSkeletons[s][i].m_RelativeQuat;
			arrSkeletons[s][i].m_AbsoluteQuat.q.Normalize();
		}

	}
}












